﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
  public  class AddressService
    {
      /// <summary>
      /// 获取省和直辖市
      /// </summary>
      /// <returns></returns>
      public DataTable GetProvince()
      {
          string sql = "select ProvinceID,ProvinceName from [dbo].TB_Add_Province";
          return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
      }
      /// <summary>
      /// 根据省获取城市
      /// </summary>
      /// <param name="provinceID"></param>
      /// <returns></returns>
      public DataTable GetCity(int provinceID)
      {
          string sql = "select CityID,CityName from [dbo].TB_Add_City where ProvinceID=" + provinceID;
          return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
      }

      /// <summary>
      /// 根据城市获取区县
      /// </summary>
      /// <param name="cityID"></param>
      /// <returns></returns>
      public DataTable GetDistrict(int cityID)
      {
          string sql = "select DistrictID,DistrictName from [dbo].TB_Add_District where CityID=" + cityID;
          return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
      }

      /// <summary>
      /// 添加地址
      /// </summary>
      /// <param name="entity"></param>
      /// <returns></returns>
      public int Edit(TB_Address entity)
      {
          SqlParameter[] parameters=new SqlParameter[]
          {
               new SqlParameter("@Add",SqlDbType.VarChar,200),
               new SqlParameter("@Tell",SqlDbType.VarChar,50),
               new SqlParameter("@UserId",SqlDbType.Int),
               new SqlParameter("@Default",SqlDbType.Int),   
               new SqlParameter("@Name",SqlDbType.VarChar,50),
          };
          parameters[0].Value = entity.Address;
          parameters[1].Value = entity.Tell;
          parameters[2].Value = entity.UserId;
          parameters[3].Value = entity.Default;
          parameters[4].Value = entity.Name;
          string sql = "insert into [dbo].[TB_Address]([address],tell,del,userid,[default],Name)values(@Add,@Tell,0,@UserId,@Default,@Name)";
          return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql,parameters);
      }

      /// <summary>
      /// 获取地址列表
      /// </summary>
      /// <returns></returns>
      public DataTable GetAddress(int userid)
      {
          string sql = "select * from TB_Address where del=0 and userid=" + userid + " order by [default] desc";
          return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
      }

      /// <summary>
      /// 设为默认地址
      /// </summary>
      /// <param name="id"></param>
      /// <returns></returns>
      public int IsDefault(int id,int userid)
      {
          string sql = "update [dbo].[TB_Address] set [default]=0 where userid="+userid+"  update [dbo].[TB_Address] set [default]=1 where id="+id+" and userid="+userid;
          return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql);
      }
    }
}
